<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: yangzg
 * @Date: 2022-08-16 16:28:32
 * @LastEditors: yangzg
 * @LastEditTime: 2022-08-30 11:10:04
-->
<template>
  <div class="container">
    <el-dialog v-model="isShow" top="15vh" width="30%" center>
      <div class="content">
        <el-tree
          :data="depList"
          :props="defaultProps"
          node-key="Id"
          :default-expanded-keys="departmentIds"
          :default-checked-keys="departmentIds"
          @check="getSelectValue"
          show-checkbox
        />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button
            @click="saveOrCancel(dialogClickType.determine)"
            type="primary"
            >确定</el-button
          >
          <el-button @click="saveOrCancel(dialogClickType.cancel)"
            >取消</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { dialogClickType, setBtnType } from "@/constants/const.ts";
import { ReorganizationData } from "@/utils/index.ts";
import {
  getApps,
  getProblemList,
  getDepList,
  getReWards,
  getOrgId,
  getArea,
} from "@/service/home/home.ts";
import { tableSearchType, getDepPrams } from "@/service/home/types.ts";
import { rewardList } from "./data.ts";

interface Props {
  detailedInfo: problemInfo;
  componentType: string;
  isEdit?: true;
  itemInfo?: temInfoType;
  AppId: string;
}

const {
  AppId,
  detailedInfo,
  problemTypeList,
  componentType,
  isEdit,
  itemInfo,
} = defineProps<Props>();

let departmentIds =
  componentType === setBtnType.handleProblems
    ? ref(detailedInfo.handlingProblems.map((item) => item.DepartmentId))
    : ref(detailedInfo.assignmentProblem.map((item) => item.DepartmentId));

const emit = defineEmits(["getChildValue"]);

const isShow = ref<boolean>(true);
const defaultProps = reactive({
  children: "children",
  label: "Name",
  id: "Id",
});
let selectNodes = ref([]);
const getSelectValue = (item, nodes) => {
  selectNodes.value = nodes.checkedNodes;
};

// 获取部门数据
let depList = ref([]);
let OrgId = ref("");
const getDepData = async () => {
  const resO = await getOrgId({
    AppId: AppId || detailedInfo.AppId,
  });
  OrgId.value = resO.IsSuccess ? resO.Data : "";
  const resD = await getDepList({
    OrgId: OrgId.value,
  });
  const data = resD.IsSuccess ? resD.Content : [];
  depList.value = ReorganizationData(data, "Id", "ParentId");
};
getDepData();

const saveOrCancel = (type) => {
  if (type === dialogClickType.determine) {
    emit("getChildValue", {
      type: componentType,
      btnType: dialogClickType.determine,
      OrgId: OrgId.value,
      list: selectNodes.value.map((item) => ({
        DepartmentId: item.Id,
        DepartmentName: item.Name,
      })),
    });
  } else {
    emit("getChildValue", {
      type: componentType,
      btnType: dialogClickType.cancel,
    });
  }
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
